/**
*
* Revision History
*
* Creation tptfc
* 
*  Modifications:
*  Name			Data			Modification
*  hfa			03/10/2009 		Added method getFile(int fileID, String cookie) 
*
*
*/
package org.syncsoft.overalt.communication.api;

import java.io.File;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultHttpClient;
import org.syncsoft.overalt.common.api.Parameter;
import org.syncsoft.overalt.communication.exception.ConnectionException;
import org.syncsoft.overalt.communication.impl.ClosedConnectionStatus;
import org.syncsoft.overalt.communication.impl.ConnectedConnectionStatus;

/**
 * 
 * @author tptfc
 *
 */
public class Connection {
	private String login;
	private String password;
	private String urlConnection;
	
	private DefaultHttpClient httpClient;
	
	private IConnectionStatus status;
	private ClosedConnectionStatus closedStatus;
	private ConnectedConnectionStatus connectedStatus;
	
	private boolean inUse;
	
	{
		setHttpClient(new DefaultHttpClient());
		setClosedStatus(new ClosedConnectionStatus(getHttpClient()));
		setConnectedStatus(new ConnectedConnectionStatus(getHttpClient()));
		setStatus(getClosedStatus());
	}
	
	/**
	 * @precondition urlConnection != null
	 * 
	 * @param urlConnection
	 */
	public Connection(String urlConnection) {
		setUrlConnection(urlConnection);
	}
	
	/**
	 * @precondition	(login && password && urlConnection) != null
	 * 
	 * @param login user server login
	 * @param password user server password
	 * @param urlConnection URL address to connect in server
	 */
	public Connection(String login, String password, String urlConnection) {
		setLogin(login);
		setPassword(password);
		setUrlConnection(urlConnection);
	}
	
	/**
	 * @throws ConnectionException
	 *  
	 * @precondition (login && password) valid user server data
	 * @postcondition isConnected == true 
	 */
	public synchronized void open() {
		if (!getStatus().isConnected()) {
			close();
			getStatus().open();
			setStatus(getConnectedStatus());
		}
	}
	
	/**
	 * @precondition getHttpClient() != null
	 * @postcondition isConnected == false
	 */
	public synchronized void close() {
		getStatus().close();
		setStatus(getClosedStatus());
	}
	
	/**
	 * @precondition connection established between server and client
	 * @postcondition execute a HTTP request to uri using the POST method
	 * 
	 * @param uri
	 * @param parameters
	 * @return the request entity
	 * 
	 * @throws ConnectionException
	 */
	public HttpResponse get(String uri, List<Parameter> parameters) {
		return getStatus().get(uri, parameters);
	}
	
	/**
	 * @precondition connection established between server and client
	 * @postcondition execute a HTTP request to uri using the POST method
	 * 
	 * @param url
	 * @param parameters
	 * @return the request entity
	 * 
	 * @throws ConnectionException
	 */
	public HttpResponse post(String uri, List<Parameter> parameters) {
		return getStatus().post(uri, parameters);
	}
	
	/**
	 * @precondition connection established between server and client
	 * @postcondition execute a HTTP request to uri using the POST method
	 * 
	 * @param uri
	 * @param parameters
	 * @param fileEntityName
	 * @param file
	 * @return
	 */
	public HttpResponse postMultipart(String uri, List<Parameter> parameters, String fileEntityName, File file) {
		return getStatus().postMultipart(uri, parameters, fileEntityName, file);
	}
	
	//	GETs AND SETs
	protected String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
		getClosedStatus().setLogin(login);
	}
	protected String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
		getClosedStatus().setPassword(password);
	}
	protected String getUrlConnection() {
		return urlConnection;
	}
	protected void setUrlConnection(String urlConnection) {
		this.urlConnection = urlConnection;
		getClosedStatus().setUrlConnection(urlConnection);
	}

	protected IConnectionStatus getStatus() {
		return status;
	}

	protected void setStatus(IConnectionStatus status) {
		this.status = status;
	}

	protected DefaultHttpClient getHttpClient() {
		return httpClient;
	}

	protected void setHttpClient(DefaultHttpClient httpClient) {
		this.httpClient = httpClient;
	}

	protected ClosedConnectionStatus getClosedStatus() {
		return closedStatus;
	}

	protected void setClosedStatus(ClosedConnectionStatus closedStatus) {
		this.closedStatus = closedStatus;
	}

	protected ConnectedConnectionStatus getConnectedStatus() {
		return connectedStatus;
	}

	protected void setConnectedStatus(ConnectedConnectionStatus connectedStatus) {
		this.connectedStatus = connectedStatus;
	}

	public boolean isInUse() {
		return inUse;
	}

	public void setInUse(boolean inUse) {
		this.inUse = inUse;
	}
}
